package com.iwanghang.animationrotatedemo20190224;

import android.animation.AnimatorSet;
import android.animation.ValueAnimator;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;

import static android.view.FrameMetrics.ANIMATION_DURATION;
import static android.view.animation.Animation.INFINITE;

public class MainActivity extends AppCompatActivity {

    ImageView[] imageViews;

    private ImageView image_oval_01;
    private ImageView image_oval_02;
    private ImageView image_oval_03;
    private ImageView image_oval_04;

    int[] enterDelay = {0, 100, 200, 300, 400, 500, 600, 500, 400};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        image_oval_01 = findViewById(R.id.image_oval_01);
        image_oval_02 = findViewById(R.id.image_oval_02);
        image_oval_03 = findViewById(R.id.image_oval_03);
        image_oval_04 = findViewById(R.id.image_oval_04);

        imageViews = new ImageView[4];

        imageViews[0] = image_oval_01;
        imageViews[1] = image_oval_02;
        imageViews[2] = image_oval_03;
        imageViews[3] = image_oval_04;

//        for (int i = 0; i < imageViews.length; i++) {
//            playEnterAnimation(imageViews[i], i); // 播放九球放大动画
//        }

//        Animation animation = new RotateAnimation(0, 360);
//        animation.setDuration(500);
//        animation.setRepeatCount(8);//动画的反复次数
//        animation.setFillAfter(true);//设置为true，动画转化结束后被应用
//        image_oval_01.startAnimation(animation);//開始动画


        //rotateAnim();

        startCountDown();
    }


    public void rotateAnim() {
        Animation anim =new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //anim.setFillAfter(true); // 设置保持动画最后的状态
        anim.setRepeatCount(8);//动画的反复次数
        anim.setDuration(3000); // 设置动画时间
        //anim.setInterpolator(new AccelerateInterpolator()); // 设置插入器
        anim.setInterpolator(new LinearInterpolator());
        image_oval_01.startAnimation(anim);
    }

    /**
     * 播放进入动画 - 位移放大
     */
    private void playEnterAnimation(final ImageView imageView, int position) {

//        AnimatorSet buttonAnimator = new AnimatorSet(); // 动画集合 按钮动画 = new 动画集合();

//        /**
//         * 01.更新alpha
//         */
//        final ValueAnimator alphaAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
//        alphaAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                imageView.setAlpha((float)alphaAnimator.getAnimatedValue());
//                imageView.requestLayout();
//            }
//        });
//        alphaAnimator.setDuration(ANIMATION_DURATION);


        Animation anim =new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

        anim.setStartOffset(position * 1000);
        anim.setRepeatCount(99999);//动画的反复次数
        //anim.setRepeatMode(INFINITE);
        anim.setDuration(4000); // 设置动画时间
        anim.setInterpolator(new LinearInterpolator());
        imageView.setVisibility(View.VISIBLE);
        imageView.startAnimation(anim);


//        /**
//         * 组合播放
//         */
//        buttonAnimator.play(anim);
//        //buttonAnimator.play(anim).with(buttonAnimatorY).with(buttonSizeAnimator).with(alphaAnimator);
//        buttonAnimator.setStartDelay(enterDelay[position]);
//        buttonAnimator.start();

    }




    /*
     * ↓↓↓↓↓↓↓↓↓↓ ---------- 扫描倒计时 ---------- ↓↓↓↓↓↓↓↓↓↓
     */
    private void startCountDown() {
        Tools.logByWh("扫描倒计时开始" );
        handler.postDelayed(runnable, 1000);
    }
    private int recLen = 0;
    private static Boolean isCountStopped = false;
    private Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            if (!isCountStopped){
                recLen++;
                Tools.logByWh("扫描倒计时:"  + recLen + "S" );
                if (recLen < 4) {

                    Animation anim =new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);

                    //anim.setStartOffset(recLen * 4000);
                    anim.setRepeatCount(99999);//动画的反复次数
                    //anim.setRepeatMode(INFINITE);
                    anim.setDuration(6666); // 设置动画时间
                    anim.setInterpolator(new LinearInterpolator());
                    imageViews[recLen].setVisibility(View.VISIBLE);
                    imageViews[recLen].startAnimation(anim);
                    handler.postDelayed(this, 1000);

                }
            }
        }
    };
    /*
     * ↑↑↑↑↑↑↑↑↑↑ ---------- 扫描倒计时 ---------- ↑↑↑↑↑↑↑↑↑↑
     */





}
